<?php
namespace app\api\controller;
use think\facade\Db;
use think\facade\Request;
class Problem
{
    //题库列表
    public function problemList()
    {   
        $pageData = Request::param();
        $page = $pageData['page'] ?? 1;
        $perPage = $pageData['pageSize'] ?? 10;
        $category_id = $pageData['category_id'] ?? '';
        $difficulty = $pageData['difficulty'] ?? '';
        $keyword = $pageData['keyword'] ?? '';

        $query = Db::name('problem')->alias('p')
                ->join('problem_category c', 'p.category_id = c.id', 'left')
                ->field('p.id, p.title, p.difficulty, c.name as category_name');

        if ($category_id) {
            $query = $query->where('p.category_id', $category_id);
        }
        if ($difficulty) {
            $query = $query->where('p.difficulty', $difficulty);
        }
        if ($keyword) {
            $query = $query->where('p.title', 'like', '%'.$keyword.'%');
        }

        $query = $query->where('p.status', '=', 1);

        $problems = $query->order('p.create_time', 'desc')
                ->paginate([
                    'list_rows' => $perPage,
                    'page'      => $page,
                ]);
        
        // 获取提交统计数据
        $stats = Db::query('SELECT 
                problem_id,
                SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS correct_count,
                COUNT(DISTINCT user_id) AS user_count,
                COUNT(*) AS submission_count,
                ROUND(SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) / COUNT(*) * 100, 2) AS correct_rate
            FROM 
                submission
            GROUP BY 
                problem_id');
        
        // 将统计数据转换为以problem_id为键的关联数组
        $statsMap = [];
        foreach ($stats as $stat) {
            $statsMap[$stat['problem_id']] = $stat;
        }
        
        // 将统计数据合并到题目列表中
        $list = $problems->toArray();
        foreach ($list['data'] as &$problem) {
            if (isset($statsMap[$problem['id']])) {
                $problem['stats'] = $statsMap[$problem['id']];
            } else {
                // 如果没有该题目的提交数据，提供默认值
                $problem['stats'] = [
                    'correct_count' => 0,
                    'user_count' => 0,
                    'submission_count' => 0,
                    'correct_rate' => 0
                ];
            }
        }
        
        if (empty($list['data'])) {
            return json([
                'message' => '没有数据', 
                'data' => '', 
                'code' => 200
            ]);
        } else {
            return json([
                'message' => '数据查询成功',
                'code'    => 200,
                'data'    => $list,
            ]);
        }
    }
    //问题详情
    public function problemDetail()
    {
        $id = Request::param('id');
        
        // 使用find()方法直接获取单条记录，返回对象或null
        $problemData = Db::name('problem')->where('id', $id)->find();
        
        if (!$problemData) {
            return json([
                'message' => '题目不存在', 
                'data' => '', 
                'code' => 404 // 建议使用404状态码表示资源不存在
            ]);
        }
        
        // 获取题目统计数据
        $stats = Db::query('SELECT 
                problem_id,
                SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS correct_count,
                COUNT(DISTINCT user_id) AS user_count,
                COUNT(*) AS submission_count,
                ROUND(SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) / COUNT(*) * 100, 2) AS correct_rate
            FROM 
                submission
            WHERE 
                problem_id = :problem_id
            GROUP BY 
                problem_id', ['problem_id' => $id]);
        
        // 处理统计数据
        if (!empty($stats)) {
            $problemData['stats'] = $stats[0];
        } else {
            // 提供默认统计数据
            $problemData['stats'] = [
                'correct_count' => 0,
                'user_count' => 0,
                'submission_count' => 0,
                'correct_rate' => 0
            ];
        }
        
        return json([
            'message' => '数据查询成功',
            'code'    => 200,
            'data'    => $problemData,
        ]);
    }
}